package com.hhh.api.module.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.houbb.sensitive.word.support.result.WordResultHandlers;
import com.github.houbb.sensitive.word.support.result.WordTagsDto;
import com.hhh.api.enums.ContentCategory;
import com.hhh.api.module.entity.BlackList;
import com.hhh.api.util.Result;
import com.hhh.api.vo.SensitiveWordVo;
import com.hhh.api.vo.Word;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "敏感词")
@RestController
@RequestMapping("/api")
public class ApiController {

    @ApiOperation(value = "标准内容检测")
    @GetMapping("/words-check")
    public Result wordsCheck(@RequestParam String word) {
        SensitiveWordVo vo = new SensitiveWordVo();
        boolean containsSensitiveWord = SensitiveWordHelper.contains(word);
        vo.setIsContains(containsSensitiveWord);

        if (!containsSensitiveWord) {
            return Result.ok(vo);
        }

        // 获取替换后的文本
        vo.setReturnStr(SensitiveWordHelper.replace(word));

        // 获取敏感词信息
        List<WordTagsDto> wordList = SensitiveWordHelper.findAll(word, WordResultHandlers.wordTags());
        List<Word> sensitiveWords = wordList.stream()
                .map(this::convertToWord)
                .collect(Collectors.toList());
        vo.setSensitiveWords(sensitiveWords);

        return Result.ok(vo);
    }

    private Word convertToWord(WordTagsDto dto) {
        Word word = new Word();
        word.setKeyWord(dto.getWord());
        if (dto.getTags() != null){
            String categories = dto.getTags().stream()
                    .map(ContentCategory::getByCode)
                    .map(ContentCategory::getDescription)
                    .collect(Collectors.joining(","));
            word.setCategory(categories);
        }
        return word;
    }
}
